﻿var companyId = '';
var ztId = '';
var kjnd = '';
var kjkm = [];
var aj = [];
var pjh = [];
var zph = [];
var wldx = [];
var xm = [];
var bm = [];
var userdefault = {};
var params = {};
var dataList = [];
var mapList = [{id: "col_kjkm", field: "kjkm"}, {id: "col_aj", field: "aj"},
    {id: "col_pjh",field: "pjh"},{id: "col_zph",field: "zph"},
    {id: "col_wldx", field: "wldx"}, {id: "col_xm", field: "xm"},
    {id: "col_bm", field: "bm"}, {id: "col_yefx1Str", field: "yefx1Str"},
    {id: "col_ncye", field: "ncye"}, {id: "col_yefx2Str", field: "yefx2Str"},
    {id: "col_qcye", field: "qcye"}, {id: "col_jfs", field: "jfs"}, {id: "col_dfs", field: "dfs"},
    {id: "col_jlj", field: "jlj"}, {id: "col_dlj", field: "dlj"},
    {id: "col_yefx3Str", field: "yefx3Str"}, {id: "col_ye", field: "ye"}];


//创建一个表格控件
var tbl = new QTable();

var bootstrap = function ($, learun) {

    var pageWidth = 1024;
    var colData2 = [];

    "use strict";
    var page = {
        init: function () {
            userdefault = learun.clientdata.get(['usersetting']);

            //初始化打印插件
            hiprint.init();
            pageWidth = ($('.lr-layout-grid').width());
            $("#divDataList").height($(window).height() - 160);


            page.bind();


        },
        bind: function () {
            $('#multiple_condition_query').lrMultipleQuery(function (queryJson) {
                page.search(queryJson);
            }, 440, 800);
            $('#pzrqMonth').lrselect({
                type: 'default',
                // 展开最大高度
                maxHeight: 300,
                placeholder: '月份',
                data: [
                    {'id': '01', 'text': '01'},
                    {'id': '02', 'text': '02'},
                    {'id': '03', 'text': '03'},
                    {'id': '04', 'text': '04'},
                    {'id': '05', 'text': '05'},
                    {'id': '06', 'text': '06'},
                    {'id': '07', 'text': '07'},
                    {'id': '08', 'text': '08'},
                    {'id': '09', 'text': '09'},
                    {'id': '10', 'text': '10'},
                    {'id': '11', 'text': '11'},
                    {'id': '12', 'text': '12'}
                ],
                select: function (item) {
                    if (!item || item.id == '-1') {
                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-" + 12 + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    } else {
                        var kjqj = item.id;
                        var beginDate = kjnd + "-" + kjqj + "-01";
                        var day = new Date(kjnd, kjqj, 0);
                        var endDate = kjnd + "-" + kjqj + "-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);
                    }
                }
            });
            // 公司选择
            $('#company_select').lrselect({
                type: 'tree',
                // placeholder: '请选择机构',
                height: 300,
                maxHeight: 300,
                // 是否允许搜索
                //allowSearch: true,
                select: function (item) {
                    if (item && item.value != '-1') {
                        companyId = item.value;
                        $('#zt_select').lrselectRefresh({
                            // 访问数据接口地址
                            url: config.base_server() + 'zts/dropdownlist?access_token=' + config.getToken().access_token,
                            // 访问数据接口参数
                            param: {companyId: companyId},
                            select: function (ztItem) {
                                if (!ztItem || ztItem.value == '-1') {
                                    ztId = "";
                                } else {
                                    ztId = ztItem.value;
                                    $("#zt_title").html(ztItem.text);
                                    $('#kjnd_select').lrselectRefresh({
                                        // 访问数据接口地址
                                        url: config.base_server() + 'kjnds/dropdownlist?access_token=' + config.getToken().access_token,
                                        // 访问数据接口参数
                                        param: {companyId: companyId, ztId: ztItem.value},
                                    }).lrselectSet(userdefault.kjnd);
                                }
                            }
                        }).lrselectSet(userdefault.ztId);
                    }
                }
            });
            $('#company_select').lrselectRefresh({
                // 访问数据接口地址
                url: config.base_server() + 'companys/tree?access_token=' + config.getToken().access_token,
                // 访问数据接口参数
                param: {parentId: '0'}
            }).lrselectSet(userdefault.companyId);
            // 帐套选择
            $('#zt_select').lrselect({
                type: 'default',
                //placeholder: '请选择帐套',
                // 展开最大高度
                maxHeight: 300
            });
            // 会计年度选择
            $('#kjnd_select').lrselect({
                type: 'default',
                //placeholder: '请选择会计年度',
                // 展开最大高度
                maxHeight: 300,
                select: function (item) {
                    if (!item || item.value == '-1') {
                        kjnd = '';
                    } else {
                        kjnd = item.value;

                        var beginDate = kjnd + "-01-01";
                        var day = new Date(kjnd, 12, 0);
                        var endDate = kjnd + "-12-" + day.getDate();
                        $("#pzrqStart").val(beginDate);
                        $("#pzrqEnd").val(endDate);

                        //凭证类型
                        $("#pzlxCode").lrselectRefresh({
                            url: config.base_server() + 'pzlxs/list?access_token=' + config.getToken().access_token,
                            param: {companyId: companyId, ztId: ztId, kjnd: kjnd, status: 1}
                        });
                        $('#kjkmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'kjkms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '科目', name: 'codeAndName', width: 225, align: 'left'},
                                {
                                    label: "余额方向", name: "yefx", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "借" : "贷";
                                    }
                                },
                                {
                                    label: "项目", name: "isXmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "部门", name: "isBmHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "案件", name: "isAjHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "票据号", name: "isPjhHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "支票号", name: "isZphHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {
                                    label: "往来", name: "isWlHs", width: 50, align: "center",
                                    formatter: function (cellvalue) {
                                        return cellvalue == 1 ? "是" : "";
                                    }
                                },
                                {label: '是否明细', name: 'isMx', width: 1, align: 'left', hidden: true},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                                {label: '简称', name: 'simpleName', width: 225, align: 'left', hidden: true}
                            ],
                            select: function (values, texts) {
                                kjkm = values;
                            }
                        });
                        $('#xmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'xms/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                xm = values;
                            }
                        });
                        $('#bmCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            reset: true,
                            url: config.base_server() + 'departments?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left'},
                            ],
                            select: function (values, texts) {
                                bm = values;
                            }
                        });
                        $('#ajCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'ajs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                aj = values;
                            }
                        });
                        $('#pjhCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=1' + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                pjh = values;
                            }
                        });
                        $('#zphCodes').lrlayerselect({
                            dataValueId: 'name',
                            dataTextId: 'name',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'pjhs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&type=2' + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'name', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                            ],
                            select: function (values, texts) {
                                zph = values;
                            }
                        });
                        $('#wldxCodes').lrlayerselect({
                            dataValueId: 'code',
                            dataTextId: 'codeAndName',
                            enableInput: true,
                            reset: true,
                            url: config.base_server() + 'wldxs/list?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&status=1' + '&access_token=' + config.getToken().access_token,
                            grid: [
                                {label: '名称', name: 'codeAndName', width: 225, align: 'left'},
                                {label: '备注', name: 'remark', width: 225, align: 'left'},
                                {label: '编号', name: 'code', width: 120, align: 'left', hidden: true},
                            ],
                            select: function (values, texts) {
                                wldx = values;
                            }
                        });

                        page.search({});
                    }
                }
            });
            //凭证类型
            $("#pzlxCode").lrselect({
                type: 'default',
                placeholder: '==请选择凭证类型==',
                id: "code",
                value: "code",
                text: "name",
                description: "",
                maxHeight: 200,
                height: 200
            });
            // 查询
            $('#btn_Search').on('click', function () {
                page.search(params);
            });

            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            //预览
            $('#lr_printview').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=fzxyeb', function (res) {
                    if (res.resp_code == 200) {
                        var temp = JSON.parse(res.data.templete);
                        temp.panels[0].printElements[4].options.columns[0] = colData2;
                         var temp2 = JSON.stringify(temp);
                        var temp3 = JSON.parse(temp2);

                        hiprintTemplate = new hiprint.PrintTemplate({
                            //template: JSON.parse(res.data.templete),
                            template: temp3,
                            paginationContainer: '.hiprint-printPagination'
                        });

                        var item = {};
                        item.pzflList = dataList;
                        if (item.pzflList.length > 72) {
                            item.pzflList = item.pzflList.slice(0, 72);
                        }
                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = kjkm.simpleName;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        //hiprintTemplate.print(item);
                        hiprintTemplate.resetPage();
                        $('#myModal .modal-body .prevViewDiv').html(hiprintTemplate.getHtml(item));
                        $('#myModal').modal('show');
                    }
                });
                $('#A4_printByHtml').click(function () {
                    hiprintTemplate.printByHtml($('#myModal .modal-body .prevViewDiv'));
                })
            });
            //打印
            $('#lr_print').on('click', function () {
                var hiprintTemplate = null;
                learun.httpAsyncGet2(config.base_server() + 'printTempletes/findByCode', "&companyId=" + companyId + '&ztId=' + ztId + '&code=fzxyeb', function (res) {
                    if (res.resp_code == 200) {
                        var temp = JSON.parse(res.data.templete);
                        temp.panels[0].printElements[4].options.columns[0] = colData2;
                        var temp2 = JSON.stringify(temp);
                        var temp3 = JSON.parse(temp2);

                        hiprintTemplate = new hiprint.PrintTemplate({
                            //template: JSON.parse(res.data.templete),
                            template: temp3,
                            paginationContainer: '.hiprint-printPagination'
                        });

                        var item = {};
                        item.pzflList = dataList;

                        item.ztName = $("#zt_select .lr-select-placeholder").html();
                        item.kjkmName = kjkm.simpleName;
                        var pzrqStart = $("#pzrqStart").val();
                        var pzrqEnd = $("#pzrqEnd").val();
                        item.pzrq = pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10);
                        hiprintTemplate.resetPage();
                        hiprintTemplate.print2(item);
                    }
                });
            });

            //导出Excel
            $("#lr_export_excel").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/fzxyeb/excel',
                    param: params
                });
            });
            //导出pdf
            $("#lr_export_pdf").on('click', function () {
                if (params.companyId == undefined) {
                    learun.alert.warning('请先查询要导出的数据！');
                    return;
                }
                params.access_token = config.getToken().access_token;

                learun.download({
                    url: config.base_server() + 'reports/fzxyeb/pdf',
                    param: params
                });
            });
        },

        search: function (param) {
            param = param || {};
            var pzrqStart = $("#pzrqStart").val();
            var pzrqEnd = $("#pzrqEnd").val();

            param.companyId = companyId;
            param.ztId = ztId;
            param.kjnd = kjnd;
            param.pzrqStart = pzrqStart;
            param.pzrqEnd = pzrqEnd;

            var formData = $('.lr-query-formcontent').lrGetFormData();

            param.kjkmEnable = formData.kjkmEnable;
            param.ajEnable = formData.ajEnable;
            param.pjhEnable = formData.pjhEnable;
            param.zphEnable = formData.zphEnable;
            param.wldxEnable = formData.wldxEnable;
            param.bmEnable = formData.bmEnable;
            param.xmEnable = formData.xmEnable;

            var kjkmEnable = formData.kjkmEnable;
            var ajEnable = formData.ajEnable;
            var pjhEnable = formData.pjhEnable;
            var zphEnable = formData.zphEnable;
            var wldxEnable = formData.wldxEnable;
            var bmEnable = formData.bmEnable;
            var xmEnable = formData.xmEnable;

            if (formData.kjkmEnable == 0 && formData.ajEnable == 0 && formData.pjhEnable == 0 && formData.zphEnable == 0 && formData.wldxEnable == 0 && formData.bmEnable == 0 && formData.xmEnable == 0) {
                learun.alert.error("至少选择一个辅助项");
                return;
            }

            param.kjkmXh = formData.kjkmXh;
            param.ajXh = formData.ajXh;
            param.pjhXh = formData.pjhXh;
            param.zphXh = formData.zphXh;
            param.xmXh = formData.xmXh;
            param.bmXh = formData.bmXh;
            param.wldxXh = formData.wldxXh;
            if ($.isArray(bm) && bmEnable == '1') {
                param.bmCodes = bm.join();
            }
            if ($.isArray(xm) && xmEnable == '1') {
                param.xmCodes = xm.join();
            }
            if ($.isArray(pjh) && pjhEnable == '1') {
                param.pjhCodes = pjh.join();
            }
            if ($.isArray(zph) && zphEnable == '1') {
                param.zphCodes = zph.join();
            }
            if ($.isArray(wldx) && wldxEnable == '1') {
                param.wldxCodes = wldx.join();
            }
            if ($.isArray(aj) && ajEnable == '1') {
                param.ajCodes = aj.join();
            }
            if ($.isArray(kjkm) && kjkmEnable == '1') {
                param.kjkmCodes = kjkm.join();
            }
            if ($("#ajCodes_input").val().trim() == "") {
                param.ajCodes = "";
            }
            if ($("#pjhCodes_input").val().trim() == "") {
                param.pjhCodes = "";
            }
            if ($("#zphCodes_input").val().trim() == "") {
                param.zphCodes = "";
            }
            if ($("#wldxCodes_input").val().trim() == "") {
                param.wldxCodes = "";
            }

            params = param;
            learun.loading(true);
            learun.httpPost2(config.base_server() + 'reports/fzxyeb_header2?access_token=' + config.getToken().access_token, param, function (res) {
                learun.loading(false);
                if (res.resp_code == 200) {
                    colData2 = res.data.colData2;
                    var col_html = "";
                    var th_html = "";
                    var tr_html = "";
                    var html = "";
                    for (var i = 0; i < res.data.colData.length; i++) {
                        col_html += "<col width=\"" + res.data.colData[i].width + "\"/>";
                        th_html += "<th>" + res.data.colData[i].label + "</th>";
                        tr_html += " <td style=\"text-align:" + res.data.colData[i].align + "\" id=\"" + res.data.colData[i].name + "\"></td>";
                    }
                    html += "<table class=\"table\">";
                    html += col_html;
                    html += "<thead style=\"background-color: lavender;\">";
                    html += "<tr>";
                    html += th_html;
                    html += "</tr>";
                    html += "</thead><tbody><tr ondblclick=\"ViewData(this)\">";
                    html += tr_html;
                    html += "</tr></tbody>";
                    html += "</table>";

                    $("#divDataList").html(html);


                    tbl.BindDom("divDataList"); //绑定到模板
                    tbl.setMaplist(mapList);  //设置数据映射关系

                    learun.httpPost(config.base_server() + 'reports/fzxyeb?access_token=' + config.getToken().access_token, param, function (res) {
                        learun.loading(false);
                        if (res.resp_code == 200) {
                            dataList = res.data;
                            for (var i = 0; i < dataList.length; i++) {
                                dataList[i].ncye = $.formatMoney(dataList[i].ncye, 2);
                                dataList[i].qcye = $.formatMoney(dataList[i].qcye, 2);
                                dataList[i].jfs = $.formatMoney(dataList[i].jfs, 2);
                                dataList[i].dfs = $.formatMoney(dataList[i].dfs, 2);
                                dataList[i].jlj = $.formatMoney(dataList[i].jlj, 2);
                                dataList[i].dlj = $.formatMoney(dataList[i].dlj, 2);
                                dataList[i].ye = $.formatMoney(dataList[i].ye, 2);
                            }
                            tbl.BindData(dataList);
                        } else {
                            learun.alert.error(res.resp_msg);
                            learun.httpErrorLog(res.resp_msg);
                        }
                    });

                } else {
                    learun.alert.error(res.resp_msg);
                    learun.httpErrorLog(res.resp_msg);
                }
            });


            $("#pzrq_title").html(pzrqStart.substring(0, 10) + "至" + pzrqEnd.substring(0, 10));
        }
    };
    page.init();
}

//查看明细账
function ViewData(row) {
    var data = row.data;
    var kjkm = data.kjkm;
    var aj = data.aj;
    var pjh = data.pjh;
    var zph = data.zph;
    var wldx = data.wldx;
    var xm = data.xm;
    var bm = data.bm;
    var pzrqStart = $("#pzrqStart").val();
    var pzrqEnd = $("#pzrqEnd").val();
    top.learun.frameTab.open({
        id: 'fzxmxz_view',
        css: 'fa fa-file-text-o',
        name: '辅助项明细账',
        path: config.webportal_server + 'pages/finance/report/fzxmxzReport2.html?companyId=' + companyId + '&ztId=' + ztId + '&kjnd=' + kjnd + '&kjkm=' + encodeURI(encodeURI(kjkm)) + '&aj=' + encodeURI(encodeURI(aj)) + '&pjh=' + encodeURI(encodeURI(pjh))+ '&zph=' + encodeURI(encodeURI(zph)) + '&wldx=' + encodeURI(encodeURI(wldx)) + '&xm=' + encodeURI(encodeURI(xm)) + '&bm=' + encodeURI(encodeURI(bm)) + '&pzrqStart=' + pzrqStart + '&pzrqEnd=' + pzrqEnd + '&access_token=' + config.getToken().access_token
    });
}